//
//  UIImageView+SDWebImageCache.swift
//  Sina
//
//  Created by YYY on 2017/8/15.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import Foundation

import SDWebImage

extension UIImageView
{
    
    /// 隔离SDWebImage 设置图像
    ///
    /// - Parameters:
    ///   - urlString: 链接地址
    ///   - placeholderImage: 占位符图片
    ///   - isAvatar: 是否是头像
    func setImage(urlString: String?,placeholderImage: UIImage?,isAvatar: Bool = false)
    {
        guard let urlString = urlString,
            let url = URL(string: urlString) else
        {
            image = placeholderImage
            return
        }
        
       
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            
            //设置头像圆角
            if isAvatar
            {
                //闭包里面使用self，避免循环引用
                self?.image = image?.avatarImage(size: self?.size)
            }
        }
    }
}
